//
//  ASVisitorView.swift
//  weiboBlog
//
//  Created by asenter on 16/3/27.
//  Copyright © 2016年 asenter. All rights reserved.
//

import UIKit

//定义协议，继承和类的格式一样，不是OC的<NSObject>,当协议方法定义为optional,协议需要加上@objc
@objc protocol ASVisitorViewDelegate:NSObjectProtocol {
    
    optional func visitorViewDidClickResgistButton()
    
    optional func visitorViewDidClickLoginButton()
}

class ASVisitorView: UIView {
    
    //定义代理属性 id<ASVisitorViewDelegate>
    //代理需要设置成weak,不然可能会循环引用
    weak var delegate:ASVisitorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setVisitor(imageName: String,message: String){
        houseView.hidden = true
        
        self.sendSubviewToBack(coverView)
        
        iconView.image = UIImage(named: imageName)
        
        messageLable.text = message
    }
    
    func startRotation() {
        
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        rotation.toValue = M_PI * 2
        rotation.duration = 20
        rotation.repeatCount = MAXFLOAT
        
        rotation.removedOnCompletion = false
        
        iconView.layer.addAnimation(rotation, forKey: "transform.rotation")
        
    }
    
    //准备UI
    private func prepareUI() {
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        addSubview(iconView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(messageLable)
        addSubview(resgistButton)
        addSubview(loginButton)
        
        //关闭autorease
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        resgistButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //item 要约束的View
        //attribute 要约束的view的属性
        //toItem 参照的view
        //relatedBy 参照。。
        //添加约束--转轮
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //遮罩
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))

        //小房子
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //消息lable
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        
        //注册按钮
        addConstraint(NSLayoutConstraint(item: resgistButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: resgistButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: resgistButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: resgistButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登陆按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
    }
    
    @objc private func didClickResgistButton(button: UIButton){
        
        delegate?.visitorViewDidClickResgistButton!()
    }
    
    func didClickLoginButton(){
        
        delegate?.visitorViewDidClickLoginButton!()
        
    }
    
    //懒加载各种页面控件
    ///转轮
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    ///遮罩
    private lazy var coverView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    ///小房子
    private lazy var houseView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    ///消息lable
    private lazy var messageLable:UILabel = {
        let lable = UILabel()
        
        lable.text = "关注一些人，看看有什么惊喜"
        lable.textAlignment = NSTextAlignment.Center
        lable.font = UIFont.systemFontOfSize(18)
        lable.numberOfLines = 0
        lable.sizeToFit()
 
        return lable
    }()
    
    ///注册按钮
    private lazy var resgistButton: UIButton = {
       
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "didClickResgistButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    ///登陆按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "didClickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    

}
